package com.hutu.erp.catalog.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hutu.erp.catalog.domain.Specification;
import com.hutu.erp.catalog.service.ISpecificationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品参数Controller
 * 
 * @author ruoyi
 * @date 2021-05-11
 */
@RestController
@RequestMapping("/catalog/specification")
public class SpecificationController extends BaseController
{
    @Autowired
    private ISpecificationService specificationService;

    /**
     * 查询商品参数列表
     */
    @PreAuthorize("@ss.hasPermi('catalog:specification:list')")
    @GetMapping("/list")
    public TableDataInfo list(Specification specification)
    {
        String username = SecurityUtils.getUsername();
        startPage();
        List<Specification> list = specificationService.selectSpecificationList(specification);
        return getDataTable(list);
    }

    /**
     * 导出商品参数列表
     */
    @PreAuthorize("@ss.hasPermi('catalog:specification:export')")
    @Log(title = "商品参数", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Specification specification)
    {
        List<Specification> list = specificationService.selectSpecificationList(specification);
        ExcelUtil<Specification> util = new ExcelUtil<Specification>(Specification.class);
        return util.exportExcel(list, "商品参数数据");
    }

    /**
     * 获取商品参数详细信息
     */
    @PreAuthorize("@ss.hasPermi('catalog:specification:query')")
    @GetMapping(value = "/{specificationId}")
    public AjaxResult getInfo(@PathVariable("specificationId") Long specificationId)
    {
        return AjaxResult.success(specificationService.selectSpecificationById(specificationId));
    }

    /**
     * 新增商品参数
     */
    @PreAuthorize("@ss.hasPermi('catalog:specification:add')")
    @Log(title = "商品参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Specification specification)
    {
        specification.setCreatedBy(SecurityUtils.getUsername());
        specification.setUpdatedBy(SecurityUtils.getUsername());
        return toAjax(specificationService.insertSpecification(specification));
    }

    /**
     * 修改商品参数
     */
    @PreAuthorize("@ss.hasPermi('catalog:specification:edit')")
    @Log(title = "商品参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Specification specification)
    {

        return toAjax(specificationService.updateSpecification(specification));
    }

    /**
     * 删除商品参数
     */
    @PreAuthorize("@ss.hasPermi('catalog:specification:remove')")
    @Log(title = "商品参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specificationIds}")
    public AjaxResult remove(@PathVariable Long[] specificationIds)
    {
        return toAjax(specificationService.deleteSpecificationByIds(specificationIds));
    }
}
